package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SjMeterReading;
import com.ruoyi.system.domain.SjWaterMeter;
import com.ruoyi.system.domain.SysFloor;
import com.ruoyi.system.mapper.WaterMeterMapper;
import com.ruoyi.system.service.IWaterMeterService;
import net.sf.jsqlparser.expression.DoubleValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class WaterMeterServiceImpl implements IWaterMeterService {

    @Autowired
    private WaterMeterMapper waterMeterMapper;

    @Autowired
    private SysFloorServiceImpl sysFloorService;

    @Override
    public List<SjWaterMeter> getWaterMeterList(SjWaterMeter sjWaterMeter) {
        return waterMeterMapper.getWaterMeterList(sjWaterMeter);
    }

    @Override
    public SjWaterMeter getWaterMeterById(String ammeterId) {
        return waterMeterMapper.getWaterMeterById(ammeterId);
    }

    @Override
    public int insertWaterMeter(SjWaterMeter sjWaterMeter) {
        //查询上期数据
        if(sjWaterMeter.getDegreeIssueA()==null){
            sjWaterMeter.setDegreeIssueA(0.0);
        }
        if(sjWaterMeter.getDegreeIssueB()==null){
            sjWaterMeter.setDegreeIssueB(0.0);
        }
        SjWaterMeter sjwm = initSjwm(sjWaterMeter);
        sjwm.setAmmeterId(UUID.randomUUID().toString().replaceAll("-",""));
        return waterMeterMapper.insertWaterMeter(sjWaterMeter);
    }

    @Override
    public int updateWaterMeter(SjWaterMeter sjWaterMeter) {
        //查询上期数据
        if (sjWaterMeter.getDegreeIssueA()==null){
            sjWaterMeter.setDegreeIssueA(0.0);
        }
        if (sjWaterMeter.getDegreeIssueB()==null){
            sjWaterMeter.setDegreeIssueB(0.0);
        }
        SjWaterMeter sjwm = initSjwm(sjWaterMeter);
        return waterMeterMapper.updateWaterMeter(sjwm);
    }

    @Override
    public int removeWaterMeter(String ids) {
        String[] ammeterIds = Convert.toStrArray(ids);
        return waterMeterMapper.removeWaterMeter(ammeterIds);
    }


    @Override
    public SjWaterMeter selectWaterMeterByFloorAndEtldt(SjWaterMeter sjWaterMeter) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String etldt = "";
        if(sjWaterMeter.getIssueDate()!=null){
            etldt = sdf.format(sjWaterMeter.getIssueDate());
        }
        sjWaterMeter.setEtldt(etldt);
        return waterMeterMapper.selectWaterMeterByFloorAndEtldt(sjWaterMeter);
    }

    @Override
    public int changeWaterMeterStatus(SjWaterMeter sjWaterMeter) {
        return waterMeterMapper.changeWaterMeterStatus(sjWaterMeter);
    }

    @Override
    public String importWaterMeter(List<SjWaterMeter> sjWaterMeterList, boolean updateSupport) {
        int size = 0;
        if(sjWaterMeterList!=null){
            size = sjWaterMeterList.size();
        }
        int doUp = 0;
        int noUp = 0;
        int errorData = 0;
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        StringBuilder doUpdates = new StringBuilder();
        StringBuilder noUpdates = new StringBuilder();
        StringBuilder errorDatas = new StringBuilder();
        for(SjWaterMeter sjWaterMeter:sjWaterMeterList){
            Date issueDate;
            try {
                issueDate = sdfDate.parse(sjWaterMeter.getExcelTmpDate());
                sjWaterMeter.setIssueDate(issueDate);
            } catch (ParseException e) {
                errorData++;
                String errStr = "缺少抄表日期或日期格式不正确；</br>";
                errorDatas.append(errStr);
                continue;
            }
            if(StringUtils.isEmpty(sjWaterMeter.getFloorName())){
                errorData++;
                String errStr = "未填写楼层；</br>";
                errorDatas.append(errStr);
                continue;
            }
            if(sjWaterMeter.getDegreeIssueA()==null){
                sjWaterMeter.setDegreeIssueA(0.0);
            }
            if(sjWaterMeter.getDegreeIssueB()==null){
                sjWaterMeter.setDegreeIssueB(0.0);
            }
            String etldt = sdf.format(issueDate);
            sjWaterMeter.setEtldt(etldt);
            //查询数据是否已有
            SjWaterMeter thisSJWM = waterMeterMapper.selectWaterMeterByFloorAndEtldt(sjWaterMeter);
            SysFloor sysFloor =sysFloorService.selectSysFloorByName(sjWaterMeter.getFloorName());
            if(thisSJWM!=null){  //本期数据已有
                if(sysFloor!=null) {
                    if(updateSupport){  //更新
                        Double allDegree = sjWaterMeter.getDegreeIssueA()+sjWaterMeter.getDegreeIssueB();
                        sjWaterMeter.setDegreeIssue(allDegree);
                        SjWaterMeter sjwm = initSjwm(sjWaterMeter);
                        sjwm.setAmmeterId(thisSJWM.getAmmeterId());
                        waterMeterMapper.updateWaterMeter(sjwm);
                        doUp++;
                        String doUpdateStr = "月份：" + etldt + "，" + "楼层：" + thisSJWM.getFloorName() + "；</br>";
                        doUpdates.append(doUpdateStr);
                    }else{  //不更新
                        noUp++;
                        String noUpdateStr = "月份：" + etldt + "，" + "楼层：" + thisSJWM.getFloorName() + "；</br>";
                        noUpdates.append(noUpdateStr);
                    }
                }else{  //楼层不存在
                    errorData++;
                    String errStr = "楼层：" + thisSJWM.getFloorName() + "不存在；</br>";
                    errorDatas.append(errStr);
                }
            }else{  //本期数据未有
                Double allDegree = sjWaterMeter.getDegreeIssueA()+sjWaterMeter.getDegreeIssueB();
                sjWaterMeter.setDegreeIssue(allDegree);
                SjWaterMeter sjwm = initSjwm(sjWaterMeter);
                sysFloor =sysFloorService.selectSysFloorByName(sjwm.getFloorName());
                if(sysFloor!=null) {
                    sjwm.setAmmeterId(UUID.randomUUID().toString().replaceAll("-",""));
                    waterMeterMapper.insertWaterMeter(sjwm);
                }else{  //楼层不存在
                    errorData++;
                    String errStr = "楼层：" + sjWaterMeter.getFloorName() + "不存在；</br>";
                    errorDatas.append(errStr);
                }
            }
        }
        String message;
        if(size>0){
            message = "共导入"+size+"条数据。</br>";
        }else{
            return "无数据";
        }
        if(StringUtils.isNotEmpty(doUpdates.toString())){
            message = message + "其中" + doUp + "条数据已有，已全部更新，楼层批次如下：</br>";
            message = message + doUpdates.toString();
        }
        if(StringUtils.isNotEmpty(noUpdates.toString())){
            message = message + "其中" + noUp + "条数据已有，未更新，楼层批次如下：</br>";
            message = message + doUpdates.toString();
        }
        if(StringUtils.isNotEmpty(errorDatas.toString())){
            message = message + "其中" + errorData + "条数据导入失败，错误原因如下：</br>";
            message = message + errorDatas.toString();
        }
        return message;
    }

    public SjWaterMeter initSjwm(SjWaterMeter sjWaterMeter){
        Date date = sjWaterMeter.getIssueDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        String oldEtldt = sdf.format(m);
        SjWaterMeter oldSjwm = new SjWaterMeter();
        oldSjwm.setFloorName(sjWaterMeter.getFloorName());
        oldSjwm.setEtldt(oldEtldt);
        SjWaterMeter oldSJWM = waterMeterMapper.selectWaterMeterByFloorAndEtldt(oldSjwm);
        Double degreePreA = 0.0;
        Double degreePreB = 0.0;
        Double degreePre = 0.0;
        //把上个月的本期度数置为本月的往期度数
        if (oldSJWM != null) {
            if (oldSJWM.getDegreeIssueA() != null) {
                degreePreA = oldSJWM.getDegreeIssueA();
            }
            if (oldSJWM.getDegreeIssueB() != null) {
                degreePreB = oldSJWM.getDegreeIssueB();
            }
            if (oldSJWM.getDegreeIssue() != null) {
                degreePre = oldSJWM.getDegreeIssue();
            }
        }
        sjWaterMeter.setDegreePreA(degreePreA);
        sjWaterMeter.setDegreePreB(degreePreB);
        sjWaterMeter.setDegreePre(degreePre);
        sjWaterMeter.setDegreeA(sjWaterMeter.getDegreeIssueA() - degreePreA);
        sjWaterMeter.setDegreeB(sjWaterMeter.getDegreeIssueB() - degreePreB);
        sjWaterMeter.setDegree(sjWaterMeter.getDegreeIssue() - degreePre);
        String etldt = sdf.format(date);
        sjWaterMeter.setEtldt(etldt);
        return sjWaterMeter;
    }
}
